package dao.mysql;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;

import javax.naming.InitialContext;
import javax.sql.DataSource;

import model.Priority;

import dao.PriorityDAO;

public class PriorityDAOImpl implements PriorityDAO{
	DataSource dataSource=null;
	Connection con=null;
	PreparedStatement pstmt=null;
	public PriorityDAOImpl(){
		try{
			//dataSource=(DataSource) new InitialContext().lookup("java:comp/env/jdbc/fbs");
			//con=dataSource.getConnection();
			con=ConnectionProperties.getConnection();
		}
		catch(Exception ee){
			System.out.println(ee.getMessage());
		}
	}

	@Override
	public void insertPriority(Priority priority) throws SQLException {
		// TODO Auto-generated method stub
		try{
		pstmt=con.prepareStatement("insert into Priority values(?,?)");
		pstmt.setInt(1, priority.getPriorityID());
		pstmt.setString(2, priority.getPriorityType());
		pstmt.executeUpdate();
		}
		catch(SQLException e){
			e.printStackTrace();
		}
		finally
		{
			pstmt.close();
			con.close();
		}
	}

	@Override
	public void updatePriority(Priority priority) throws SQLException {
		// TODO Auto-generated method stub
		try{
			pstmt=con.prepareStatement("update Priority set PriorityType=? where PriorityID=?");
			pstmt.setString(1, priority.getPriorityType());
			pstmt.setInt(2, priority.getPriorityID());
			pstmt.executeUpdate();
		}
		catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		finally
		{
			pstmt.close();
			con.close();
		}
		
	}

	@Override
	public void deletePriority(int PriorityID) throws SQLException {
		// TODO Auto-generated method stub
		try{
		pstmt=con.prepareStatement("delete from Priority where PriorityID=?");
		pstmt.setInt(1, PriorityID);
		pstmt.executeUpdate();
		}
		catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		finally
		{
			pstmt.close();
			con.close();
		}
	}

	@Override
	public Priority findPriority(int PriorityID) throws SQLException {
		// TODO Auto-generated method stub
		Priority p=null;
		try{
			pstmt=con.prepareStatement("select * from Staff where PriorityID=?");
			pstmt.setInt(1, PriorityID);
			ResultSet rs=pstmt.executeQuery();
			if(rs.next()){
				p=new Priority();
				p.setPriorityID(rs.getInt("PriorityID"));
				p.setPriorityType(rs.getString("PriorityType"));
			}
		}
		catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		finally
		{
			pstmt.close();
			con.close();
		}
		
		return p;
	}

	@Override
	public ArrayList<Priority> findAllPriority() throws SQLException {
		// TODO Auto-generated method stub
		ArrayList<Priority> pArrayList=new ArrayList<Priority>();
		try{
			pstmt=con.prepareStatement("select * from Priority");
			ResultSet rs=pstmt.executeQuery();
			while(rs.next()){
				Priority p=new Priority();
				p.setPriorityID(rs.getInt("PriorityID"));
				p.setPriorityType(rs.getString("PriorityType"));
				pArrayList.add(p);
			}
		}
		catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		finally
		{
			pstmt.close();
			con.close();
		}
		return pArrayList;
		
	}

	@Override
	public ArrayList<String> findAllPriorityNames() {
		// TODO Auto-generated method stub
		ArrayList<String> pArrayList=new ArrayList<String>();
		try{
			pstmt=con.prepareStatement("select PriorityType from Priority");
			ResultSet rs=pstmt.executeQuery();
			while(rs.next()){
				pArrayList.add(rs.getString("PriorityType"));
			}
		}
		catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		finally
		{
			try {
				pstmt.close();
				con.close();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			
		}
		return pArrayList;
	}
	
	@Override
	public void deletebyPriorityType(String PriorityType) throws SQLException {
		// TODO Auto-generated method stub
		try{
		pstmt=con.prepareStatement("delete from Priority where PriorityType=?");
		pstmt.setString(1, PriorityType);
		pstmt.executeUpdate();
		}
		catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		finally
		{
			pstmt.close();
			con.close();
		}
	}

	

}
